// pages/course/course.js
const API = require("../../http/API")
const http = require("../../http/http")
Page({
	data: {
		fenlei:[],
		page:1,
		limit:10,
		type:1,
		title:"",
		list:[],
		course_subjects_ids:"",
		show:false,
		arr:['最新','入门','进阶','免费','图文','兑换','专题'],
		currentindex:0,
		tishi:""
	},
	onLoad(options) {
		http(API.course).then(res=>{
			this.setData({
				fenlei:res.data
			})
		})
		this.clickquanbu()
	},
	change(){
		clearTimeout(this.timeout)
		this.setData({
			page:1
		})
		this.timeout = setTimeout(()=>{
			if(this.data.type != 1){
				this.getlist()
			}else {
				this.clickquanbu()
			}
		},500)
	},
	clickfenlei(e){
		this.setData({
			course_subjects_ids:e.currentTarget.id,
			type:"",
			page:1
		})
		this.getlist()
	},
	getlist(){
		http(API.courseone,{
			course_subjects_ids:this.data.course_subjects_ids,
			page:this.data.page,
			limit:this.data.limit,
			title:this.data.title,
			type:1
		}).then(res=>{
			if (this.data.page == 1){
				this.setData({
					list:res.data.data,
				})
			}else {
				if (res.data.data.length){
					this.setData({list:this.data.list.concat(res.data.data)})
				}else {
		        wx.showToast({
			          title: '没有更多数据',
			          icon: 'none'
			        })
				}
			}
		})
	},
	clickquanbu(){
		this.setData({
			type:1,
			course_subjects_ids:"",
		})
		http(API.courseone,{
			type:this.data.type,
			page:this.data.page,
			limit:this.data.limit,
			title:this.data.title
		}).then(res=>{
			if (this.data.page == 1){
				this.setData({
					list:res.data.data,
					tishi:"未找到符合条件的课程"
				})
			}else {
				if (res.data.data.length){
					this.setData({list:this.data.list.concat(res.data.data)})
				}else {
		        wx.showToast({
			          title: '没有更多数据',
			          icon: 'none'
			        })
				}
			}
		})
	},
	gengduo(){
		this.setData({
			show:!this.data.show
		})
	},
	clicktitle(e){
		this.setData({
			currentindex:e.currentTarget.id
		})
	},
	onReachBottom(){
		this.setData({page:this.data.page * 1 + 1})
		if(this.data.type === 1 ){
			this.clickquanbu()
		}else {
			this.getlist()
		}
	}
})